/**
 * General Exception Handler.
 *
 * Static ExceptionHandler used to register exceptions/error to be caught
 * and to call custom exception handlers registered by listeners.
 *
 * A listener has to implement a "except(e)" method, that takes a standard
 * Javascript Error or custom Exception instance as a parameter.
 */
function	ExceptionHandler()
{
  this.exceptions	= {};	// registered exceptions and their listeners


  /**
   * Internal function to check if the developer has defined his own
   * Exception class.
   */
  var	exceptionExists 	= function()
  {
    return ((typeof(Exception) != 'undefined') && (Exception != 'undefined'));
  }

  /**
   * Registers the given Exception/Error.
   *
   * @param e			The Exception/Error to be registered.
   */
  this.registerException	= function(e)
  {
    if ((typeof(e) == 'function') &&
    	(typeof(e.name) != 'undefined') &&
    	(e.name != 'undefined'))
    {
      this.exceptions[e.name] = new Array();
      return (true);
    }
    return (false);
  }

  /**
   * Adds/hooks a given Listener on the given Exception/Error.
   *
   * @param e			The Exception/Error to hook the listener on.
   * @param listener	The Listener object.
   */
  this.addListener			= function(e, listener)
  {
    if ((typeof(e) == 'function') &&
    	(typeof(e.name) != 'undefined') &&
    	(e.name != 'undefined') &&
    	listener)
    {
      this.exceptions[e.name].push(listener);
      return (true);
    }
    return (false);
  }

  /**
   * Removes/unhooks the given Listener from the given Exception/Error.
   *
   * @param e			The Exception/Error to hook the listener on.
   * @param listener	The Listener object.
   */
  this.removeListener		= function(e, listener)
  {
	if ((e &&
       ((e instanceof Error) ||
        (exceptionExists() && (e instanceof Exception)))) &&
        (typeof(e.name) != 'undefined') && e.name &&
        this.exceptions[e.name] &&
        listener)
    {
//		delete this.exceptions[e.name][listener];
      return (true);
    }
    return (false);
  }

  /**
   * Tracking method to be called when an Exception/Error
   * is being caught.
   *
   * for instance:
   * try
   * {
   * 	[...]
   * }
   * catch (e)
   * {
   * 	ExceptionHandler.tracker(e);
   * }
   *
   * @param e			The Exception/Error to hook the listener on.
   * @param listener	The Listener object.
   */
  this.tracker				= function(e)
  {
	if ((e &&
        ((e instanceof Error) ||
         (exceptionExists() && (e instanceof Exception)))) &&
         (typeof(e.name) != 'undefined') &&
        e.name && this.exceptions[e.name])
    {
      for (i = 0; i < this.exceptions[e.name].length; i++)
      {
        this.exceptions[e.name][i].except(e);
      }
      return (true);
    }
    return (false);
  }
}


/*
** Overriding the constructor to have it pose as a static namespace.
*/
var	ExceptionHandler = new ExceptionHandler();
